import 'dart:async';

import 'package:inmotion_pro/pages/mixin/cache_mixin.dart';
import 'package:inmotion_pro/pages/mixin/check_mixin.dart';
import 'package:inmotion_pro/pages/mixin/http_request_mixin.dart';
import 'package:inmotion_pro/routes/app_routes.dart';
import 'package:get/get.dart';
import 'package:inmotion_pro/service/guest_service.dart';
import 'package:inmotion_pro/service/launch_service.dart';
import 'state.dart';

// 启动页控制器
class LaunchController extends GetxController
    with CacheMixin, HttpRequestMixin, CheckMixin {
  // 状态
  final LaunchState state = LaunchState();

  @override
  Future<void> onInit() async {
    super.onInit();

    // 防止游客模式下，用户直接关闭应用后再次打开时，仍然是游客模式
    await GuestService.to.leaveGuestMode();
    // 等待检查车辆缓存完成
    bool hasWaited = await LaunchService.to.checkAuthorizedVehicle();
    if (!hasWaited) {
      // 为了保证用户体验，至少等待1秒钟再跳转，如果已经等待过了则不需要再等待
      await Future.delayed(const Duration(milliseconds: 1000));
    }
    await Get.offAllNamed(AppRoutes.ROOT);
  }
}
